<template>
    <el-dialog :visible.sync="open" @close="close" :title="title" width="800px" append-to-body destory-on-close>
        <el-form ref="elForm" :model="formData" size="small" label-width="100px" label-position="left">
            <el-row :gutter="15">
                <el-col :span="24">
                    <el-form-item label="审批人" prop="dept">
                        <span :style="{ marginRight: '15px' }" v-if="formData.userName != null">{{ formData.userName }}</span>
                        <el-button type="primary" size="mini" @click="handlePickRole">选择审批人</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="handleConfirm">确定</el-button>
        </div>
        <staff-choose-dialog ref="staffChoose" :muilt="false" @Unitpersonnel="onStaffChoose" />
    </el-dialog>
</template>
<script>
import dialogMixin from "@/mixins/dialogMixin";
import StaffChooseDialog from "@/views/components/staffChooseDialog/index";
import * as Auth from "@/utils/auth";
import { submitDrillPlanApprove, submitBatchDrillPlanApprove } from "@/api/drill/drill";

export default {
    name: "DrillPlanApprove",
    mixins: [dialogMixin],
    components: { StaffChooseDialog },
    data() {
        return {
            formData: {
                planId: 0,
                planIds: [],
                userId: 0,
                userName: "",
            },
            userName: "",
            title: "提交演练计划审批",
            muilt: false,
        };
    },
    methods: {
        show(id) {
            // 如果id是数组
            if (Array.isArray(id)) {
                this.formData.ids = id;
                this.muilt = true;
            } else {
                this.formData.id = id;
            }
            this.open = true;
        },
        handleConfirm() {
            if (this.formData.userId == 0) {
                this.$modal.msgError("请选择审批人");
                return false;
            }
            let userId = Auth.getUserId();
            if (userId == this.formData.userId) {
                this.$modal.msgError("不能选择自己");
                return false;
            }
            if (this.muilt) {
                submitBatchDrillPlanApprove({
                    planIds: this.formData.ids,
                    userId: this.formData.userId,
                    userName: this.formData.userName,
                }).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess("演练计划审批提交成功");
                        this.$emit("handleOk");
                        this.close();
                    } else {
                        this.$modal.msgError("演练计划审批提交失败");
                    }
                });
            } else {
                submitDrillPlanApprove(this.formData.id, { userId: this.formData.userId, userName: this.formData.userName }).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess("演练计划审批提交成功");
                        this.$emit("handleOk");
                        this.close();
                    } else {
                        this.$modal.msgError("演练计划审批提交失败");
                    }
                });
            }
        },
        handlePickRole() {
            this.$refs.staffChoose.showDialog("选择审批人");
        },
        onStaffChoose(deptId, userId, user, dept) {
            this.formData.userId = userId;
            this.formData.userName = user.userName;
        },
    },
};
</script>
<style></style>
